﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YJKArch.ElementCollisionChecking
{
	public partial class CollisionCheckingResultForm : System.Windows.Forms.Form
	{
		public CollisionCheckingResultForm(CollisionCheckingManager manager)
		{
			this.m_Manager = manager;
			this.InitializeComponent();
		}

		private void CollisionCheckingResultForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
			this.UpdateFormData();
		}

		private void CollisionCheckingResultForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
			this.m_Manager.UnHighlightElements();
		}

		private void btnReChecking_Click(object sender, EventArgs e)
		{
			try
			{
				this.m_Manager.CollisionChecking();
				this.UpdateFormData();
			}
			catch (Exception)
			{
			}
		}

		private void treeListView_DoubleClick(object sender, EventArgs e)
		{
			if (this.treeListView.SelectedItems.Count < 1)
			{
				return;
			}
			TreeListViewItem treeListViewItem = this.treeListView.SelectedItems[0];
			try
			{
				int num = Convert.ToInt32(treeListViewItem.Tag);
				if (num >= 0)
				{
					this.SetFocusToRevit();
					this.m_Manager.ZoomToElement(num, this.chbDCZoom.Checked);
					this.SetFocusToFormSelf();
				}
			}
			catch (Exception)
			{
			}
		}

		private void treeListView_Click(object sender, EventArgs e)
		{
			if (this.treeListView.SelectedItems.Count < 1)
			{
				return;
			}
			TreeListViewItem treeListViewItem = this.treeListView.SelectedItems[0];
			try
			{
				int num = Convert.ToInt32(treeListViewItem.Tag);
				if (num >= 0)
				{
					this.SetFocusToRevit();
					this.m_Manager.ZoomToElement(num, false);
					this.SetFocusToFormSelf();
				}
			}
			catch (Exception)
			{
			}
		}

		private void UpdateFormData()
		{
			this.treeListView.Items.Clear();
			TreeListViewItem treeListViewItem = TreeListViewEditor.AddRootItem("墙", 0, this.treeListView);
			treeListViewItem.Tag = -1;
			TreeListViewItem treeListViewItem2 = TreeListViewEditor.AddRootItem("梁", 0, this.treeListView);
			treeListViewItem2.Tag = -1;
			TreeListViewItem treeListViewItem3 = TreeListViewEditor.AddRootItem("板", 0, this.treeListView);
			treeListViewItem3.Tag = -1;
			TreeListViewItem treeListViewItem4 = TreeListViewEditor.AddRootItem("柱", 0, this.treeListView);
			treeListViewItem4.Tag = -1;
			int num = 0;
			int num2 = num;
			this.UpdateTreeListViewByType(treeListViewItem, this.m_Manager.WallCollisionCheckingResult, ref num);
			num2 = num - num2;
			if (num2 == 0)
			{
				treeListViewItem.ImageIndex = 1;
			}
			int num3 = num;
			this.UpdateTreeListViewByType(treeListViewItem2, this.m_Manager.BeamCollisionCheckingResult, ref num);
			num3 = num - num3;
			if (num3 == 0)
			{
				treeListViewItem2.ImageIndex = 1;
			}
			int num4 = num;
			this.UpdateTreeListViewByType(treeListViewItem3, this.m_Manager.FloorCollisionCheckingResult, ref num);
			num4 = num - num4;
			if (num4 == 0)
			{
				treeListViewItem3.ImageIndex = 1;
			}
			int num5 = num;
			this.UpdateTreeListViewByType(treeListViewItem4, this.m_Manager.ColumnCollisionCheckingResult, ref num);
			num5 = num - num5;
			if (num5 == 0)
			{
				treeListViewItem4.ImageIndex = 1;
			}
			this.labelCount.Text = string.Concat(new string[]
			{
				"共检查到",
				num2.ToString(),
				"处墙重叠",
				num3.ToString(),
				"处梁重叠",
				num4.ToString(),
				"处板重叠",
				num5.ToString(),
				"处柱重叠"
			});
		}

		private void UpdateTreeListViewByType(TreeListViewItem root, List<ComponentInfo> components, ref int rowIndex)
		{
			int num = 0;
			foreach (ComponentInfo componentInfo in components)
			{
				int num2 = num + 1;
				num = num2;
				TreeListViewItem treeListViewItem = TreeListViewEditor.AddItem(num2.ToString(), 1, root);
				TreeListViewEditor.AddColumnItem(new List<string>
				{
					componentInfo.ComponentId.ToString(),
					componentInfo.ComponentTypeName,
					componentInfo.LevelName,
					componentInfo.CollisionComponentId.ToString()
				}, treeListViewItem);
				treeListViewItem.Tag = rowIndex;
				rowIndex++;
			}
		}

		private void CollisionCheckingResultForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			CollisionCheckingResultForm.SendMessage(CmdCollisionChecking._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void SetFocusToFormSelf()
		{
			int wMsg = 7;
			CollisionCheckingResultForm.SendMessage(base.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			CollisionCheckingResultForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			CollisionCheckingResultForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "CollisionCheckingResultForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.chbDCZoom.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string path = Path.GetTempPath() + "CollisionCheckingResultForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.chbDCZoom.Checked = true;
				return true;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			try
			{
				this.chbDCZoom.Checked = Convert.ToBoolean(streamReader.ReadLine());
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return true;
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private CollisionCheckingManager m_Manager;
	}
}
